package org.xtremeobjects.domain.states;

import java.util.ArrayList;
import java.util.List;

import org.xtremeobjects.domain.states.Action;
import org.xtremeobjects.domain.states.Event;
import org.xtremeobjects.domain.states.Guard;
import org.xtremeobjects.domain.states.State;
import org.xtremeobjects.domain.states.StateMachine;

/**
 * @author    Juan
 */
public class Transition {

	private StateMachine stateMachine;
	private State from;
	private State to;
	/**
	 * @uml.property name="guard"
	 * @uml.associationEnd aggregation="composite" inverse="transition:org.xtremeobjects.domain.aspects.states.Guard"
	 */
	private Guard guard;
	private Event event;
	/**
	 * @uml.property  name="actions"
	 */
	private List<Action> actions;
	
	public Transition(StateMachine stateMachine, State from, State to) {
		this.stateMachine = stateMachine;
		this.from = from;
		this.to = to;
	}

	public Event event(String name) {
		Event ev = findEventByName(name);
		if(ev==null)
			ev = createEvent(name);
		return event(ev);
	}

	private Event createEvent(String name) {
		return stateMachine.createEvent(name);
	}
	
	private Event findEventByName(String name) {
		return stateMachine.findEventByName(name);
	}

	public Event event(Event event) {
		this.event = event;
		return event;
	}

	public State getTo() {
		return this.to;
	}

	public State getFrom() {
		return this.from;
	}
	
	public Guard getGuard() {
		return this.guard;
	}
	
	public Event getEvent() {
		return this.event;
	}

	public Transition guard(String expression, String description) {
		this.guard = new Guard(expression, description);
		return this;
	}

	public Transition addAction(String string) {
		getActions().add(new Action(string));
		return this;
	}

	List<Action> getActions() {
		if (actions == null)
			actions = new ArrayList<Action>();
		return actions;
	}


}
